package com.hbue.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbue.springboot.pojo.TbHistory;
import com.hbue.springboot.pojo.TbWorker;
import org.apache.ibatis.annotations.*;

import java.sql.Date;
import java.util.List;

public interface TbWorkerMapper extends BaseMapper<TbWorker> {

    /**
     * 查询送水工的送水订单
     */
    @Select("select * from tb_worker")
    @Results({
            @Result(column = "id" , property = "id"),
            @Result(column = "id" , property = "workersList" , javaType = List.class,
                    many = @Many(select = "com.hbue.springboot.mapper.TbHistoryMapper.selectByWorkerId"))
    })
    public List<TbWorker> selectTick();

    @Select("select id from tb_worker where worker_name = #{workerName}")
    Integer selectByName(String workerName);

    @Select("select * from tb_history where worker_id is null and send_water_time is null and version is null")
    IPage<TbHistory> selectOrderWithoutWorker(Page<TbHistory> page);

    @Select("select * from tb_history where worker_id = #{id} and version is null")
    IPage<TbHistory> selectOrderWithWorker(Page<TbHistory> page,@Param("id") Integer id);

    @Update("update tb_history set worker_id = #{workerId} where id = #{id}")
    Integer updateWorkerId(@Param("id") Integer id, @Param("workerId") Integer workerId);

    @Update("update tb_history set send_water_time = #{date} where id = #{id}")
    Integer ensureOrder(@Param("id") Integer id,@Param("date")  Date date);
}
